package 优先算法.递归_搜索与回溯.二叉树深度搜索.求根节点到叶节点数字之和;

/**
 *
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        Solution.TreeNode root = new Solution.TreeNode(1);
//        root.left = new Solution.TreeNode(2);
        root.right = new Solution.TreeNode(3);
        int rel = so.sumNumbers(root);
        System.out.println(rel);
    }

}
class Solution {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public int sumNumbers(TreeNode root) {
        return fun(root,0);
    }
    public static int fun(TreeNode root,int preSum){
        preSum = preSum * 10 + root.val;
        if(root.left == null && root.right == null){
            return preSum;
        }
        int ret = 0;
        if(root.left != null){
            ret+= fun(root.left,preSum);
        }
        if(root.right != null){
            ret+= fun(root.right,preSum);
        }
        return ret;
    }
}